Unlocking the Power of R as Your Calculator

Basic Arithmetic Operations

Masumbuko Semba

2024-01-31

Learning Agenda

  1. Get familiar with R and Rstudio
  2. Data structure and data types
  3. Reading and writing data in Rstudio
  4. Tidying and Data manipulation with tidyverse
  5. Plotting and Visualization
  6. Descriptive Statistics
  7. Inferential Statistics
  8. Modelling and simulation
  9. Spatial Handling and Analysis

R as Your Calculator

IUsing R as Caculator

  • R follows the BODMAS order operations
  • Perform familiar arithmetic operations — addition, subtraction, multiplication, and division with ease.
  • Use built-in functions like +, -, *, and / for intuitive calculations.
  • Explore advanced operators like ^ for exponentiation and %% for modulo (remainder).

Note

The console, which is the brain of R language is where you type the expressions of the arithmetic operations

Addition

  • combine numbers using the + operator
  • store the result in a variable for later use

Warning

Demonstration of entering the command in the R console and obtaining the result.

Examples

10+15
[1] 25
15+32
[1] 47

Substraction

  • substract values using the - operator
  • assign the result to a variable

Examples

50-35
[1] 15
10-48
[1] -38

Multiplication

  • Multiply numbers using * operator
  • Store the product in a variable
  • R handles multiplication of larger dataset efficiently

Examples

4*8
[1] 32
8*7*2
[1] 112

Division

  • Divide numbers using / operator

example

25/5
[1] 5

Note

Be mindful of potential division by zero errors

4/0
[1] Inf
0/4
[1] 0

Exponentiation

  • Raise the power of numbers using ^ operator
  • Store the product in a variable
  • R handles power of larger dataset efficiently

Examples

2^3
[1] 8
8^4
[1] 4096

Square Root

  • Square the number using sqrt() function
  • Store the product in a variable
  • R handles power of larger dataset efficiently

Examples

sqrt(81)
[1] 9
sqrt(625)
[1] 25

Order of Operations

  • Use parentheses to control the order of operations.
  • Example: (5 + 3) * 2 first calculates 5 + 3, then multiplies by 2.
  • Without parentheses, R follows PEMDAS.
  • Example: 5 + 3 * 2 first multiplies 3 by 2, then adds 5.
  • Both examples return different results, showcasing the importance of order.

Note

Be mindful of division by zero, leading to an infinite error.

5 + 3 * 2/0
[1] Inf

Note

Check for mismatched parentheses or missing brackets causing syntax errors.

5 + 3) * 2

Thanks!

Slides created via the R packages: Quarto

The chakra comes from remark.js, knitr, and R Markdown.

References